package com.fleet.project01.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fleet.project01.activity.PostDetailActivity;
import com.fleet.project01.R;
import com.fleet.project01.data.model.Post;

import java.util.ArrayList;
import java.util.List;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
    private List<Post> posts;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(Post post);
    }

    public PostAdapter() {
        this.posts = new ArrayList<>();
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts != null ? posts : new ArrayList<>();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_post, parent, false);
        return new PostViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Post post = posts.get(position);
        holder.bind(post);
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    class PostViewHolder extends RecyclerView.ViewHolder {
        private TextView tvPostId;
        private TextView tvUserId;
        private TextView tvPostTitle;
        private TextView tvPostBody;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            tvPostId = itemView.findViewById(R.id.tvPostId);
            tvUserId = itemView.findViewById(R.id.tvUserId);
            tvPostTitle = itemView.findViewById(R.id.tvPostTitle);
            tvPostBody = itemView.findViewById(R.id.tvPostBody);

            itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        onItemClickListener.onItemClick(posts.get(position));
                    }
                }
                
                // 直接跳转到文章详情页面
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    Post post = posts.get(position);
                    PostDetailActivity.start(itemView.getContext(), post.getId(), post.getTitle());
                }
            });
        }

        public void bind(Post post) {
            // 设置文章ID
            tvPostId.setText("ID: " + post.getId());

            // 设置用户ID
            tvUserId.setText("用户: " + post.getUserId());

            // 设置标题
            String title = post.getTitle();
            if (title == null || title.trim().isEmpty()) {
                tvPostTitle.setText("无标题");
            } else {
                // 首字母大写
                tvPostTitle.setText(capitalizeFirstLetter(title));
            }

            // 设置内容
            String body = post.getBody();
            if (body == null || body.trim().isEmpty()) {
                tvPostBody.setText("暂无内容");
            } else {
                // 替换换行符为空格，便于显示
                String formattedBody = body.replace("\n", " ").trim();
                tvPostBody.setText(formattedBody);
            }
        }

        /**
         * 首字母大写
         */
        private String capitalizeFirstLetter(String text) {
            if (text == null || text.isEmpty()) {
                return text;
            }
            return text.substring(0, 1).toUpperCase() + text.substring(1);
        }
    }
}
